import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";
export const useUserStore = defineStore({
  id: "geeker-user",
  state: (): UserState => ({
    token: "",
    userInfo: {
		name:'',
		avatar:'',
		id:'',
		menuId:"",
	}
  }),
  getters: {},
  actions: {
	setMenuId(id){
		this.userInfo.menuId=id;
	},
    // Set Token
	logOut(){
		this.token = '';
		this.userInfo.name="";
		this.userInfo.avatar="";
	},
	setUser(data:object){
		this.token = data.access_token;
		this.userInfo.name=data.username;
		this.userInfo.avatar=data.head_portrait;
		this.userInfo.id=data.id;
		this.userInfo.org_title=data.org_title;
		this.userInfo.org_department=data.org_department;
		this.userInfo.org_company=data.org_company;
		this.userInfo.org_title_ru=data.org_title_ru;
		this.userInfo.org_department_ru=data.org_department_ru;
		this.userInfo.org_company_ru=data.org_company_ru;
		this.userInfo.refresh_access_token=data.refresh_access_token;
	},
    setToken(token: string) { 
      this.token = token;
	  if(token == ""){
		  this.userInfo.name="";
		  this.userInfo.avatar="";
	  }
    },
    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    }
  },
  persist: piniaPersistConfig("geeker-user")
});
